package a09_贪心算法;

import java.util.Arrays;

/**
 * <p>
 * a18_无重叠区间复习1
 * </p>
 *
 * @author flyduck
 * @since 2025/1/16
 */
public class a18_无重叠区间复习1 {
    public int eraseOverlapIntervals(int[][] intervals) {
        //左边界排序
        intervals = Arrays.stream(intervals)
                .sorted((item1,item2) -> {
                    if(item1[0] > item2[0]){
                        return 1;
                    }else if(item1[0] > item2[0]){
                        return -1;
                    }else {
                        return 0;
                    }
                }).toArray(int[][]::new);

        int result = 0;
        for (int i = 1; i < intervals.length; i++) {
            if(intervals[i][0] < intervals[i-1][1]){//重叠
                result++;
                intervals[i][1] = Math.min(intervals[i][1], intervals[i-1][1]);
            }
        }
        return result;
    }
}
